package com.litidea.actions;

import static com.litidea.commons.keys.MailKeys.MAIL_SUBJECT_NEW_EXTERNAL_CHANGE;
import static com.litidea.commons.keys.MailKeys.MAIL_SUBJECT_REQUEST_WAITING_FOR_SET_VERIFY_RULE;
import static com.litidea.commons.keys.SystemKeys.PUB_ADD_OPTION;
import static com.litidea.commons.keys.SystemKeys.REPORT_UPLOAD_URL;
import static com.litidea.commons.paramkeys.ChangeParamKeys.SAVE_EXTERNAL_CHANGE_PARAMS;

import java.util.Map;

import com.litidea.dao.ExternalChangeDAO;
import com.litidea.dataobjects.UserLoginDO;
import com.opensymphony.xwork2.Action;

public class ExternalChangeManageAction extends BaseAction implements Action {

	/**
	 * 序列码
	 */
	private static final long serialVersionUID = 968674751758541095L;

	private ExternalChangeDAO externalChangeDAO;
	
	/**
	 * 外部变更列表
	 * 
	 * @return
	 */
	public String getExternalChanges() {
		Map<String, String> params = getParameters(SAVE_EXTERNAL_CHANGE_PARAMS);
		try {
			fillPageParams(params);

			return responseAjax(externalChangeDAO.getExternalChangePageList(params)
					.toString());
		} catch (Exception e) {
			errorInfo = e.getMessage();
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 编辑内部变更
	 * 
	 * @return
	 */
	public String saveExternalChange() {
		Map<String, String> params = getParameters(SAVE_EXTERNAL_CHANGE_PARAMS);
		String success = null, errorInfo = null;
		try {
			int type = Integer.valueOf(params.get("type"));
			switch (type) {
			case PUB_ADD_OPTION:
				// 添加操作人员信息
				UserLoginDO userDO = this.findUserInfo();
				params.put("appliedBy", String.valueOf(userDO.getId()));
				
				String saveFileFullName = this.saveUploadFile(REPORT_UPLOAD_URL);
				params.put("externalVerifyReportURL", saveFileFullName);
				
				Integer externalChangeId = this.externalChangeDAO.addNewExternalChange(params);
				
				this.emailNotification(String.valueOf(externalChangeId), params.get("evaDeptMgrId"),
						MAIL_SUBJECT_NEW_EXTERNAL_CHANGE,
						"新的外部变更申请已提交，等待您组织人员评审");
				
				success = "添加外部变更成功";
				
				break;
			}
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return normalResponse(errorInfo, success);
	}
	
	/**
	 * 设置验证组
	 * 
	 * @return
	 */
	public String setVerifyTeam() {
		Map<String, String> params = getParameters(SAVE_EXTERNAL_CHANGE_PARAMS);
		String success = null, errorInfo = null;
		try {
			success = externalChangeDAO.setVerifyTeam(params);
			
			// 邮件通知--验证科室科长组织验证人员
			String text = "新的外部变更等待您验证。";
			this.emailNotification(params.get("id"), params
					.get("evaLeaderId"), MAIL_SUBJECT_REQUEST_WAITING_FOR_SET_VERIFY_RULE, text);
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return normalResponse(errorInfo, success);
	}

	public ExternalChangeDAO getExternalChangeDAO() {
		return externalChangeDAO;
	}

	public void setExternalChangeDAO(ExternalChangeDAO externalChangeDAO) {
		this.externalChangeDAO = externalChangeDAO;
	}

}
